package com.shux.usermanager.utils;

import java.util.ArrayList;
import java.util.List;

import com.shux.usermanager.entity.SysMenu;

/**
 **********************************************
 *  描述：处理菜单树
 * Simba.Hua
 * 2017年6月18日
 **********************************************
**/
public class MenuTreeUtil {
    private List<SysMenu> sysMenus = new ArrayList<SysMenu>();
    public MenuTreeUtil(List<SysMenu> sysMenus){
        this.sysMenus = sysMenus;
    }
    
    /**
     * 
     * @param nodeId
     * @return
     */
     public SysMenu getNodeById(Integer nodeId){
         SysMenu treeNode = new SysMenu();
         for (SysMenu item : sysMenus) {
           if (item.getMenuId().equals(nodeId)) {
             treeNode = item;
             break;
           }
         }
       return treeNode;
     }
     
     /**
     * 
     * @param nodeId
     * @return
     */
     public List<SysMenu> getChildrenNodeById(Integer nodeId){
       List<SysMenu> childrenTreeNode = new ArrayList<SysMenu>();
       sysMenus.forEach((item)->{
           if(item.getParentId().intValue() == nodeId.intValue()){
               childrenTreeNode.add(item);
           }
       });
       return childrenTreeNode;
     }
     
     /**
     * 递归生成Tree结构数据
     * @param rootId
     * @return
     */
     public SysMenu generateTreeNode(Integer rootId){
       SysMenu root = this.getNodeById(rootId);
       List<SysMenu> children = root.getSubSysMenu();
       if(children == null){
         root.setSubSysMenu(new ArrayList<SysMenu>());
       }
       List<SysMenu> childrenTreeNode = this.getChildrenNodeById(rootId);
       childrenTreeNode.forEach((item)->{
           SysMenu node = this.generateTreeNode(item.getMenuId());
           item.setTarget("treeFrame");
           root.getSubSysMenu().add(node);
       });
       return root;
     }
}
